<?php 
class App_model_router extends Model {
    //private $ACCESS_MODE = array('user', 'public', 'position');
    /*const ACCESS_MODE_USER = 'user';
    const ACCESS_MODE_PUBLIC = 'public';
    const ACCESS_MODE_POSITION = 'position';
    const ACCESS_MODE_PRIVATE = 'private';*/
    
    private $table = 'router';
    private $table_position = 'router_position';
    
    
    public function add($module, $action, $access_mode = '', $position = 0) {
        $module = trim($module);
        $action = trim($action);
        
        if ($this->get_access_mode($module, $action)) {
            return false;
        }
        
        $access_mode = trim($access_mode);
        
        $access_mode = $this->_parse_access_mode($access_mode);
        if (empty($access_mode)) {
            $access_mode = self::ACCESS_MODE_PRIVATE;
        }
        return $this->db->insert($this->table, array(
            'module'        => $module,
            'action'        => $action,
            'access_mode'   => $access_mode
        ));
    }
    
    
    public function access($router_id, $position_ids) {
        
    }
    
    public function set_position($id, $position_id) {
        $id = (int) $id;
        $position_id = (int) $position_id;
        $access_mode = $this->get_access_mode_by_id($id);
        if ( ! $access_mode) {
            return false;
        }
        $this->db->trans_start();
        if ($access_mode != self::ACCESS_MODE_POSITION) {
            $this->chage_access_mode($id, self::ACCESS_MODE_POSITION);
        }
        if ( ! $this->model('router_position')->add($id, $position_id)) {
            $this->db->trans_rollback();
            return false;
        }
        $this->db->trans_commit();
        return true;
    }
    
    
    public function info($module, $action) {
        $module = trim($module);
        $action = trim($action);
        $module = $this->db->escape_str($module);
        $action = $this->db->escape_str($action);
        
        $sql = "SELECT `id`, `access_mode` FROM `{$this->table}` WHERE `module`='{$module}' AND `action`='{$action}' LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return $row;
        } else {
            return false;
        }
    }
    
    public function get_access_mode($module, $action) {
        $module = trim($module);
        $action = trim($action);
        $module = $this->db->escape_str($module);
        $action = $this->db->escape_str($action);
        
        $sql = "SELECT `access_mode` FROM `{$this->table}` WHERE `module`='{$module}' AND `action`='{$action}' LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return $row['access_mode'];
        } else {
            return false;
        }
    }
    
    public function get_access_mode_by_id($id) {
        $id = (int) $id;
        if ($id > 0) {
            $sql = "SELECT `access_mode` FROM `{$this->table}` WHERE `id`='{$id}' LIMIT 1";
            if ($row = $this->db->row($sql)) {
                return $row['access_mode'];
            }
        }
        return false;
    }
    
    public function change_access_mode($id, $access_mode = '') {
        $access_mode = trim($access_mode);
        $access_mode = $this->_parse_access_mode($access_mode);
        
        if (empty($access_mode)) {
            tos_die("access mode cannot be none");
        }
        
        $id = (int) $id;
        if ($id === 0) {
            $this->db->error("router_id cannot be 0");
        }
        
        return $this->db->update(
            $this->table, 
            array('access_mode' => $access_mode),
            array('id' => $router_id)
        );
    }
    
    private function _parse_access_mode($access_mode) {
        if ($access_mode != self::ACCESS_MODE_POSITION && 
                $access_mode != self::ACCESS_MODE_USER &&
                $access_mode != self::ACCESS_MODE_PUBLIC &&
                $access_mode != self::ACCESS_MODE_PRIVATE) {
            $access_mode = '';
        }
        return $access_mode;
    }
}
?>